package cn.wolfcode.web.controller;


import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequiredPermission(name = "字典明细页面", expression = "systemDictionaryItem:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo) {
        //查询所有的字典明细数据
        model.addAttribute("dics",systemDictionaryService.listAll());
        //高级查询
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "systemDictionaryItem/list";
    }


    @RequiredPermission(name = "字典明细删除", expression = "systemDictionaryItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            systemDictionaryItemService.delete(id);
        }
        return "redirect:/systemDictionaryItem/list";
    }


    @RequiredPermission(name = "字典明细新增/编辑", expression = "systemDictionaryItem:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        if (systemDictionaryItem.getId() == null) {
            systemDictionaryItemService.save(systemDictionaryItem);
        } else {
            systemDictionaryItemService.update(systemDictionaryItem);
        }
        return "redirect:/systemDictionaryItem/list";
    }


    @RequiredPermission(name = "字典明细新增/编辑", expression = "systemDictionaryItem:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("systemDictionaryItem", systemDictionaryItemService.get(id));
        }
        return "systemDictionaryItem/input";
    }


    //根据目录id查询明细
    @RequestMapping("/selectByTypeId")
    @ResponseBody
    public List<SystemDictionaryItem> selectByTypeId(Long id){
        return systemDictionaryItemService.selectByTypeId(id);
    }



    @RequestMapping("/selectByTypeSn")
    @ResponseBody
    public List<SystemDictionaryItem> selectByTypeId(String sn){
        return systemDictionaryItemService.selectByTypeSn(sn);
    }

    @RequestMapping("/selectByParentId")
    @ResponseBody
    public List<SystemDictionaryItem> selectByParentId(Long id){
        return systemDictionaryItemService.selectByParentId(id);
    }
}
